package com.baidumap.administrator.happystudy.service;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;

import com.ab.download.SystemParams;
import com.ab.http.HttpUtil;
import com.ab.view.dialog.StyledDialog;
import com.activeandroid.ActiveAndroid;
import com.activeandroid.Configuration;
import com.baidu.mapapi.SDKInitializer;
import com.baidumap.administrator.happystudy.BuildConfig;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.model.GoodsInfo;
import com.baidumap.administrator.happystudy.model.InCart;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.socks.library.KLog;
import com.tencent.bugly.crashreport.CrashReport;

/**
 * 描述：
 * 作者：xiaoxing on 17/4/17 11:00
 * 邮箱：2235445233@qq.com
 */
public class InitializeService extends IntentService {

    private static final String ACTION_INIT_WHEN_APP_CREATE = "com.anly.githubapp.service.action.INIT";

    public InitializeService() {
        super("InitializeService");
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, InitializeService.class);
        intent.setAction(ACTION_INIT_WHEN_APP_CREATE);
        context.startService(intent);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_INIT_WHEN_APP_CREATE.equals(action)) {
                performInit();
            }
        }
    }

    private void performInit() {
        HttpUtil.setServiceUrl(Constant.DOMAIN_API);
        SystemParams.init(getApplicationContext());
        initKLog();

        // 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
        SDKInitializer.initialize(getApplicationContext());

        StyledDialog.init(getApplicationContext());

        //imageload初始化
        initImageLoader();

        initActiveAndroid();
        initBugly();
//        CrashReport.testJavaCrash();
    }

    private void initImageLoader() {
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true).cacheOnDisc(true).build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext()).defaultDisplayImageOptions(
                defaultOptions).build();
        ImageLoader.getInstance().init(config);
    }

    /**
     * 初始化Bugly
     */
    private void initBugly() {
        CrashReport.initCrashReport(getApplicationContext(), Constant.BUGLY_APPID, false);
    }

    /**
     * 初始化数据库
     *
     * @return
     */
    private void initActiveAndroid() {
        //        ActiveAndroid.initialize(this,true); //在5.0系统上不支持这种初始化方式

        Configuration configuration = new Configuration.Builder(getApplicationContext())
                .setDatabaseName("test.db")
                .setDatabaseVersion(1)
                .setModelClasses(InCart.class, GoodsInfo.class)
                .create();

        ActiveAndroid.initialize(configuration, true);
    }


    /**
     * 初始化KLog工具
     */
    private void initKLog() {
        KLog.init(BuildConfig.LOG_DEBUG);
    }
}